import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'login',
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
      path: '/welcome',
      name: 'welcome',
      component: () => import(/* webpackChunkName: "welcome" */ '../components/welcome.vue')
    }, {
      path: '/users',
      name: 'users',
      component: () => import(/* webpackChunkName: "users" */ '../components/user/Users.vue'),
    },
   {
      path: '/role',
      name: 'role',
      component: () => import(/* webpackChunkName: "role" */ '../components/Roles/Rolesvue'),
    },
   {
      path: '/rights',
      name: 'rights',
      component: () => import(/* webpackChunkName: "rights" */ '../components/Roles/Rights.vue'),
    },
   {
      path: '/categories',
      name: 'categories',
      component: () => import(/* webpackChunkName: "categories" */ '../components/goods/Categories.vue'),
    },
   {
      path: '/params',
      name: 'params',
      component: () => import(/* webpackChunkName: "params" */ '../components/goods/Params.vue'),
    },
   {
      path: '/goods',
      name: 'goods',
      component: () => import(/* webpackChunkName: "goods" */ '../components/goods/Goods.vue'),
    },
   {
      path: '/goods/add',
      name: 'add',
      component: () => import(/* webpackChunkName: "goods" */ '../components/goods/Add.vue'),
    },
   {
      path: '/orders',
      name: 'orders',
      component: () => import(/* webpackChunkName: "orders" */ '../components/order/Orders.vue'),
    },
   {
      path: '/reports',
      name: 'reports',
      component: () => import(/* webpackChunkName: "reports" */ '../components/report/Reports.vue'),
    },
  ]
  }, {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "Login" */ '../views/Login.vue')
  },
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  let token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})
export default router